package com.zhuiyun.educations.api.order.service.impl;

import com.github.pagehelper.PageHelper;
import com.zhuiyun.educations.api.order.entity.DTO.OrderEntityDTO;
import com.zhuiyun.educations.api.order.entity.Order;
import com.zhuiyun.educations.api.order.mapper.OrderMapper;
import com.zhuiyun.educations.api.order.service.OrderService;
import com.zhuiyun.educations.common.response.CommonResult;
import com.zhuiyun.educations.util.pageUtil.MybatisPageHelper;
import com.zhuiyun.educations.util.pageUtil.PageRequest;
import com.zhuiyun.educations.util.pageUtil.PageResult;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @ClassName OrderServiceImpl
 * @Description TODO 订单管理
 * @Author ys
 * @Date 2023/6/19 19:45
 **/
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper orderMapper;
    /**
     * @Author ys
     * @Description TODO 分页查询
     * @Date 2023/6/26 14:09
     * @Return CommonResult
     * @param
     */
    @Override
    public CommonResult getFilePrice(PageRequest pageRequest) {
        List<Order> filePrice = orderMapper.getFilePrice(pageRequest);
        for(int i=0;i<filePrice.size();i++){
            Order order = filePrice.get(i);
            int residueTime = Integer.parseInt(order.getResidueTime());
            System.out.println(residueTime);
            if(order.getOrderState()==0 && residueTime<=0){
                Integer id = order.getId();
                System.out.println("11111111"+id);
                orderMapper.update(id);
            }
            System.out.println("111"+order.getResidueTime());
        }
        PageHelper.startPage(pageRequest);
        PageResult pageResult = MybatisPageHelper.getPageResult(orderMapper.getFilePrice(pageRequest));
        return CommonResult.ok(pageResult);
    }

    @Override
    public CommonResult getFilePriceById(Integer courseId, Integer userId) {
        return CommonResult.ok(orderMapper.getFilePriceById(courseId,userId));
    }

    @Override
    public CommonResult getorderById(Integer courseId, Integer userId) {
        return CommonResult.ok(orderMapper.getorderById(courseId,userId));
    }

    /**
     * @Author cjl
     * @Description TODO 前端查询用户订单
     * @Date 2023/7/5 17:25
     * @Return com.zhuiyun.educations.common.response.CommonResult
     * @param userId
     * @param orderState
     */
    @Override
    public CommonResult selectOrderEntityDTO(Integer userId, Integer orderState) {
        List<OrderEntityDTO> list = orderMapper.selectOrderEntityDTO(userId,orderState);
        return CommonResult.ok(list);
    }

    @Override
    public CommonResult add(Order order) {
        Date date = new Date();
        order.setCreateTime(date);
        order.setDelFlag(0);
        order.setUpdateTime(date);
        order.setOrderState(0);
        order.setPaymentType("支付宝");
        order.setCloseTime(date);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(order.getCloseTime()); // 设置初始值时间
        calendar.add(Calendar.SECOND, 120);
        Date newCloseTime = calendar.getTime();
        order.setCloseTime(newCloseTime); // 将修改后的时间赋值回去

        return CommonResult.ok(orderMapper.add(order));
    }

    @Override
    public CommonResult update(Integer id) {
        System.out.println("1111");
        return  CommonResult.ok(orderMapper.update(id));
    }

    @Override
    public CommonResult updatestate(Integer id) {
        return CommonResult.ok(orderMapper.updatestate(id));
    }
}
